package com.lianxi.Courses.controller;

import com.github.pagehelper.PageInfo;
import com.lianxi.Courses.constant.ResponseCodeConstant;
import com.lianxi.Courses.entity.User;
import com.lianxi.Courses.exception.*;
import com.lianxi.Courses.service.GoldPointsService;
import com.lianxi.Courses.service.UserService;
import java.util.List;

import com.lianxi.Courses.util.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;

/**
 * 翔哥
 * 2025年10月11日
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private GoldPointsService goldPointsService;

    @RequestMapping("/login")
    public ModelAndView login(String loginName, String password,String code, HttpSession session) {
        ModelAndView mav = new ModelAndView();
        // 获取图片中的验证码信息
        String image = (String) session.getAttribute("code");

        User user = null;

        try {
            user = userService.login(loginName, password, code, image);
            session.setAttribute("user", user);
            mav.setViewName("redirect:/showMain");
        }  catch (CodeErrorException e) {
            mav.addObject("UserMsg",e.getMessage());
            mav.setViewName("backend/back_login");
        } catch (UserErrorExcetion e) {
            mav.addObject("UserMsg",e.getMessage());
            mav.setViewName("backend/back_login");
        }catch (MD5ErrorException e) {
            mav.addObject("UserMsg",e.getMessage());
            mav.setViewName("backend/back_login");
        } catch (UserAdminErrorExcetion e) {
            mav.addObject("UserMsg",e.getMessage());
            mav.setViewName("backend/back_login");
        }catch (RequestParameterErrorException e) {
            mav.addObject("UserMsg",e.getMessage());
            mav.setViewName("backend/back_login");
        }
        catch (Exception e) {
            e.printStackTrace();
            mav.addObject("UserMsg", "服务器内部异常");
            mav.setViewName("backend/back_login");
        }
        return mav;
    }

    @RequestMapping("/findByParams")
    public ModelAndView findByParams(User userParam,String pageNo, String pageSize) {
        ModelAndView mav = new ModelAndView();

        PageInfo<User> userPage = userService.findByParams(userParam, pageNo, pageSize);
        mav.addObject("userPage",userPage);
        mav.setViewName("backend/back_userSet");

        // 将条件参数再次传递到前台
        mav.addObject("userParam",userParam);

        return mav;
    }

    @RequestMapping("/findById")
    public ResponseResult findById(String id) {
        ResponseResult result =new ResponseResult();
        try {
            User user =userService.findById(id);
            result.setMessage("成功");
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_SUCCESS);
            result.setReturnObject(user);
        } catch (UserErrorExcetion e) {
            result.setMessage(e.getMessage());
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_REQUEST_PARAMETER_ERROR);
        } catch (RequestParameterErrorException e) {
            result.setMessage(e.getMessage());
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_REQUEST_PARAMETER_ERROR);
        }catch (Exception e) {
            result.setMessage("服务器内部异常");
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_FAIL);
        }
        return result;
    }

    @RequestMapping("/updateUser")
    public ResponseResult updateUser(String id ,String nickname ,String role,String password,String email) {
        ResponseResult result =new ResponseResult();

        try {
            userService.updateUser(id, nickname, role, password, email);
            result.setMessage("成功");
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_SUCCESS);
        } catch (RequestParameterErrorException e) {
            result.setMessage(e.getMessage());
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_REQUEST_PARAMETER_ERROR);
        } catch (UserErrorExcetion e) {
            result.setMessage("用户或密码错误");
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_REQUEST_PARAMETER_ERROR);
        }catch (Exception e) {
            result.setMessage("该管理员不存在");
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_FAIL);
        }
        return result;
    }

    @RequestMapping("/updateStatus")
    public ResponseResult updateStatus(String id ) {
        ResponseResult result =new ResponseResult();

        try {
            userService.updateStatus(id);
            result.setMessage("成功");
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_SUCCESS);
        } catch (RequestParameterErrorException e) {
            result.setMessage(e.getMessage());
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_REQUEST_PARAMETER_ERROR);
        } catch (Exception e) {
            result.setMessage("服务器内部异常");
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_FAIL);
        }
        return result;
    }


    @RequestMapping("/UserRegister")
    public ResponseResult UserRegister(User user) {
        ResponseResult result = new ResponseResult();
        try {
            userService.register(user);
            result.setMessage("注册成功");
            result.setReturnObject(user);
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_SUCCESS);
        } catch (RequestParameterErrorException e) {
            result.setMessage(e.getMessage());
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_REQUEST_PARAMETER_ERROR);
        } catch (Exception e) {
            e.printStackTrace();
            result.setMessage("服务器内部异常");
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_FAIL);
        }
        return result;
    }


    @RequestMapping("/frontRegister")
    @ResponseBody
    public ResponseResult frontRegister(User user, String confirmPassword) {
        ResponseResult result = new ResponseResult();
        try {
            userService.frontRegister(user, confirmPassword);
            result.setMessage("注册成功");
            result.setReturnObject(user);
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_SUCCESS);
        } catch (RequestParameterErrorException e) {
            result.setMessage(e.getMessage());
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_REQUEST_PARAMETER_ERROR);
        } catch (UserErrorExcetion e) {
            result.setMessage(e.getMessage());
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_REQUEST_PARAMETER_ERROR);
        } catch (MD5ErrorException e) {
            result.setMessage(e.getMessage());
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_FAIL);
        } catch (Exception e) {
            e.printStackTrace();
            result.setMessage("服务器内部异常");
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_FAIL);
        }
        return result;
    }







    @RequestMapping("/logout")
    public ModelAndView logout(HttpSession session) {
        session.removeAttribute("user");
        ModelAndView mav = new ModelAndView();
        mav.setViewName("redirect:/showIndex");
        return mav;
    }


    @RequestMapping("/frontLogin")
    @ResponseBody
    public ResponseResult frontLogin(String loginName, String password, HttpSession session) {
        ResponseResult result = new ResponseResult();

        try {
            User user = userService.frontLogin(loginName, password);
            User newUser =goldPointsService.findGoldPoints(String.valueOf(user.getId()),session);

            session.setAttribute("user", newUser);

            result.setMessage("登录成功");
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_SUCCESS);
            result.setReturnObject(newUser);
        } catch (UserErrorExcetion e) {
            result.setMessage(e.getMessage());
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_REQUEST_PARAMETER_ERROR);
        } catch (RequestParameterErrorException e) {
            result.setMessage(e.getMessage());
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_REQUEST_PARAMETER_ERROR);
        } catch (Exception e) {
            e.printStackTrace();
            result.setMessage("服务器内部异常");
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_FAIL);
        }
        return result;
    }





    @RequestMapping(value = "/UserInformationUpdate", method = RequestMethod.POST)
    public ResponseResult updateUserProfile(String id, String oldPassword, String newPassword, String confirmPassword, String nickname, String email) {
        ResponseResult result = new ResponseResult();

        try {
            userService.UserInformationUpdate(id, oldPassword, newPassword, confirmPassword, nickname, email);
            result.setMessage("个人信息更新成功");
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_SUCCESS);
        } catch (RequestParameterErrorException e) {
            result.setMessage(e.getMessage());
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_REQUEST_PARAMETER_ERROR);
        } catch (UserErrorExcetion e) {
            result.setMessage(e.getMessage());
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_REQUEST_PARAMETER_ERROR);
        } catch (MD5ErrorException e) {
            result.setMessage(e.getMessage());
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_FAIL);
        } catch (Exception e) {
            e.printStackTrace();
            result.setMessage("服务器内部异常");
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_FAIL);
        }

        return result;
    }

    @RequestMapping("/signIn")
    public ResponseResult signIn(String userId) {
        ResponseResult result = new ResponseResult();
        try {
            userService.signIn(userId);
            // 签到成功后，查询更新后的用户信息并返回
            User updatedUser = userService.findById(userId);
            result.setReturnObject(updatedUser);
            result.setMessage("签到成功，获得5积分");
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_SUCCESS);
        } catch (RequestParameterErrorException e) {
            result.setMessage(e.getMessage());
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_REQUEST_PARAMETER_ERROR);
        } catch (UserErrorExcetion e) {
            result.setMessage(e.getMessage());
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_REQUEST_PARAMETER_ERROR);
        } catch (Exception e) {
            e.printStackTrace();
            result.setMessage("服务器内部异常");
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_FAIL);
        }

        return result;
    }






}
